//
//  XQIdentityAccureWayController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/23.
//

#import "XQIdentityAccureWayController.h"
#import "XQIdentityAccureView.h"
#import "XQIdentityErrorController.h"

@interface XQIdentityAccureWayController ()
@property (nonatomic,strong) UIView *mainView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) XQCustomEngeLabel *topLabel;
@property (nonatomic,strong) XQIdentityAccureView *recommendView;
@property (nonatomic,strong) XQIdentityAccureView *otherView;
@end

@implementation XQIdentityAccureWayController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeUI];
    [self makeConstraints];
    
    self.startInterval = [XQTimeTool nowTimeStamp];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewIsAppearing:animated];
    [self startLocation];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self stopLocation];
}

- (void)makeUI {
    
    self.view.backgroundColor = kCardColor;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.mainView];
    [self.mainView addSubview:self.topLabel];
    [self.mainView addSubview:self.recommendView];
    [self.mainView addSubview:self.otherView];
}

- (void)makeConstraints {
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].topHeight);
        make.left.right.equalTo(@0);
        make.bottom.offset(-[UIScreen mainScreen].bottomHeight);
    }];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        make.width.mas_equalTo([UIScreen mainScreen].width);
    }];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(44);
    }];
    [self.recommendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabel.mas_bottom).offset(20);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
    }];
    [self.otherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.recommendView.mas_bottom).offset(20);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.bottom.equalTo(@-20);
    }];
}

#pragma mark - setter

- (void)setTopTitle:(NSString *)topTitle {
    [self.navigationBar setTitle:topTitle];
}

#pragma mark - private

- (void)didSelectClick:(NSString *)type {
    
    self.endInterval = [XQTimeTool nowTimeStamp];
    
    XQIdentityErrorController *vc = [XQIdentityErrorController new];
    vc.isPhoto = self.isPhoto;
    vc.topTitle = self.navigationBar.title;
    vc.maneuvers = type;
    [self.navigationController pushViewController:vc animated:YES];
    
    [self reportRisk];
}

- (void)reportRisk {
    [XQGoogleMarket reportRiskWithType:@"2" longitude:self.longitude latitude:self.latitude startTime:self.startInterval endTime:self.endInterval orderId:@""];
}

#pragma mark - lazy

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
    }
    return _mainView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
    }
    return  _scrollView;
}


- (XQCustomEngeLabel *)topLabel {
    if (!_topLabel) {
        _topLabel = [XQCustomEngeLabel new];
        _topLabel.text = @"Select an i d to verify your identity";
        _topLabel.textColor = [UIColor whiteColor];
        _topLabel.font = [UIFont systemFontOfSize:16];
        _topLabel.backgroundColor = kOrangeColor;
        _topLabel.layer.cornerRadius = 15;
        _topLabel.layer.masksToBounds = YES;
        _topLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topLabel;
}

- (XQIdentityAccureView *)recommendView {
    if (!_recommendView) {
        _recommendView = [[XQIdentityAccureView alloc] initWithTitle:@"Recommended ID Type" dataSource:self.preferredDataSource];
        [_recommendView setArrowShow:YES];
        @weakify(self)
        _recommendView.didSelectHandle = ^(NSString * _Nonnull type) {
            @strongify(self);
            [self didSelectClick:type];
        };
    }
    return _recommendView;
}

- (XQIdentityAccureView *)otherView {
    if (!_otherView) {
        _otherView = [[XQIdentityAccureView alloc] initWithTitle:@"Other Options" dataSource:self.alternativeDataSource];
        [_otherView setArrowShow:NO];
        @weakify(self)
        _otherView.didSelectHandle = ^(NSString * _Nonnull type) {
            @strongify(self);
            [self didSelectClick:type];
        };
    }
    return _otherView;
}



@end
